#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Collections.Generic;
using Flynn.Core;
using Flynn.Core.TaskResults;
using Flynn.TaskServices;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Tests.TaskServices
{
    [TestFixture]
    public class CheckDatabaseTaskServiceFixture
    {
        private MockRepository _mocks;

        private CheckDatabaseTaskService _checkDatabaseTaskService;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _checkDatabaseTaskService = new CheckDatabaseTaskService();
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Executing_Engine_Must_Not_Be_Null()
        {
            _checkDatabaseTaskService.Execute(null, new string[] {});
        }

        [Test]
        public void Project_File_Must_Be_Specified()
        {
            string output = _checkDatabaseTaskService.Execute(new Engine(), new string[] {});

            Assert.AreEqual("Failure: No project file specified", output);
        }

        [Test]
        public void ConnectionString_Must_Be_Specified()
        {
            string output = _checkDatabaseTaskService.Execute(new Engine(), new string[] {"/project:MyProject.xml"});

            Assert.AreEqual("Failure: No connection string specified", output);
        }

        [Test]
        public void Can_Execute()
        {
            IEngine engine = _mocks.CreateMock<IEngine>();

            IList<Update> updates = new List<Update>();

            updates.Add(new Update("1"));
            updates.Add(new Update("2"));

            using (_mocks.Record())
            {
                Expect.Call(engine.CheckDatabase("MyProject.xml", "ConnectionString")).Return(CheckingDatabaseResult.Success("2"));
            }

            using (_mocks.Playback())
            {
                string ouput = _checkDatabaseTaskService.Execute(engine, new string[] { "/project:MyProject.xml", "/connection:ConnectionString" });

                string expectedOuput = "Current version: 2";

                Assert.AreEqual(expectedOuput, ouput);
            }
        }

        [Test]
        public void Executing_Fails_When_Checking_Database_Fails()
        {
            IEngine engine = _mocks.CreateMock<IEngine>();

            using (_mocks.Record())
            {
                Expect.Call(engine.CheckDatabase("MyProject.xml", "ConnectionString")).Return(CheckingDatabaseResult.Failure("Failure message"));
            }

            using (_mocks.Playback())
            {
                string ouput = _checkDatabaseTaskService.Execute(engine, new string[] { "/project:MyProject.xml", "/connection:ConnectionString" });

                Assert.AreEqual("Failure: Failure message", ouput);
            }
        }
    }
}